package com.xoa.dev.keyYouth.service;


import com.xoa.dev.keyYouth.dao.PAdolescentMapper;
import com.xoa.dev.keyYouth.model.PAdolescent;
import com.xoa.model.users.Users;
import com.xoa.service.sys.SysLogService;
import com.xoa.util.ToJson;
import com.xoa.util.common.session.SessionUtils;
import com.xoa.util.page.PageParams;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class PAdolescentService {

    @Resource
    private PAdolescentMapper pAdolescentMapper;
    @Resource
    private SysLogService sysLogService;


    public ToJson addPAdolescent(HttpServletRequest request, PAdolescent record){
        ToJson json = new ToJson();
        try {
            //从session中获取当前登录人的信息
            Users users = SessionUtils.getSessionInfo(request.getSession(), Users.class, new Users());
            record.setCreatPerson(users.getUserName());
            record.setCreatDepart(users.getDeptName());
            record.setCreatData( new Date());
            int count = pAdolescentMapper.insertSelective(record);
           // sysLogService.setPzLog(request,users.getUserId(),new Date(),"1","8","重点青少年信息");
            if (count>0){
                json.setMsg("ok");
                json.setFlag(0);
            }
        }catch (Exception e){
            json.setMsg("no");
            json.setFlag(1);
        }
        return json;
    }
    public ToJson selectPAdolescent(Integer page, Integer pageSize, Boolean useFlag, PAdolescent record) {
        ToJson json = new ToJson();
        Map<String,Object> map = new HashMap<String, Object>();
        PageParams pageParams = new PageParams();
        pageParams.setPage(page);
        pageParams.setPageSize(pageSize);
        pageParams.setUseFlag(useFlag);
        map.put("page",pageParams);
        map.put("zz",record);
        try {
            List list = pAdolescentMapper.lists(map);
            if (list!=null) {
                json.setTotleNum(pageParams.getTotal());
                json.setObj(list);
                json.setMsg("ok");
                json.setFlag(0);
            }
        } catch (Exception e) {
            e.printStackTrace();
            json.setFlag(1);
        }
        return json;
    }
    public ToJson updatePAdolescent(HttpServletRequest request,PAdolescent record) {
        ToJson json = new ToJson();
        try {
            //从session中获取当前登录人的信息
            Users users = SessionUtils.getSessionInfo(request.getSession(), Users.class, new Users());
            record.setUpdateDepart(users.getDeptName());
            record.setUpdatePerson(users.getUserName());
            record.setUpdateData(new Date());
            pAdolescentMapper.updateByPrimaryKeySelective(record);
           // sysLogService.setPzLog(request,users.getUserId(),new Date(),"3","8","重点青少年信息");
            json.setFlag(0);
            json.setMsg("ok");
        }catch (Exception e){
            e.printStackTrace();
            json.setFlag(1);
        }
        return json;
    }
    public ToJson selectInofPAdolescent(Integer id) {
        ToJson json = new ToJson();
        try {
            PAdolescent pAdolescent = pAdolescentMapper.selectByPrimaryKey(id);
            json.setObject(pAdolescent);
            json.setFlag(0);
            json.setMsg("ok");
        }catch (Exception e){
            e.printStackTrace();
            json.setFlag(1);
        }
        return json;
    }
    public ToJson deletePAdolescent(HttpServletRequest request,Integer id) {
        ToJson json = new ToJson();
        try {
            Users users = SessionUtils.getSessionInfo(request.getSession(), Users.class, new Users());
            pAdolescentMapper.deleteByPrimaryKey(id);
            //sysLogService.setPzLog(request,users.getUserId(),new Date(),"2","8","重点青少年信息");
            json.setFlag(0);
            json.setMsg("ok");
        }catch (Exception e){
            e.printStackTrace();
            json.setFlag(1);
        }
        return json;
    }
}
